package cn.ymatrix.tableserver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TableService {

    // t1 表的实现批量方法
    public StringBuffer tableT1Service(JSONObject jsonObject, int batchSize) {
        StringBuffer ddlsql = new StringBuffer();
        JSONObject cdcdata = JSON.parseObject(jsonObject.getString("cdcdata"));
        String ddl_type = jsonObject.getString("type");
        String tablename = jsonObject.getString("table");
        for (int i = 0; i <= batchSize; i++) {
            // 插入数据
            if (ddl_type.equals("insert")) {
                String insertSQL = insertSQL(cdcdata, tablename);
                ddlsql.append(insertSQL);
                break;
            }
            // 更新数据
            if (ddl_type.equals("update")) {
                String updateSQL = updateSQL(cdcdata, tablename);
                ddlsql.append(updateSQL);
                break;
            }
            // 删除数据
            if (ddl_type.equals("delete")) {
                String deleteSQL = deleteSQL(cdcdata, tablename);
                ddlsql.append(deleteSQL);
                break;
            }

        }

        return ddlsql;
    }


    public String insertSQL(JSONObject cdcdata, String tablename) {
        String id = cdcdata.getString("id");
        String name = cdcdata.getString("name");
        return ("insert into " + tablename + " values(" + id + ",'" + name + "');-");
    }

    public String updateSQL(JSONObject cdcdata, String tablename) {
        String id = cdcdata.getString("id");
        String name = cdcdata.getString("name");
        return ("update " + tablename + " set name = " + name + " where id = " + id + ";-");
    }

    public String deleteSQL(JSONObject cdcdata, String tablename) {
        String deletesql = "";
        String id = cdcdata.getString("id");
        String name = cdcdata.getString("name");
        if (!id.equals("") && !name.equals("")) {
            deletesql = ("delete from " + tablename + " where id = " + id + " and name = " + name + " ;-");
        }

        if (id.equals("")) {
            deletesql = ("delete from " + tablename + " where name = " + name + " ;-");
        } else {
            deletesql = ("delete from " + tablename + " where id = " + id + " ;-");
        }
        System.out.println("delete---:" + deletesql);
        return deletesql;
    }


}
